package com.knowledgegraph.web.controller.zhongyao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.knowledgegraph.neo4j.domain.GraphNode;
import com.knowledgegraph.neo4j.service.IGraphNodeService;
import com.knowledgegraph.neo4j.service.INeo4jNodeService;
import com.knowledgegraph.zhongyao.service.IPrescriptionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.knowledgegraph.common.annotation.Log;
import com.knowledgegraph.common.core.controller.BaseController;
import com.knowledgegraph.common.core.domain.AjaxResult;
import com.knowledgegraph.common.enums.BusinessType;
import com.knowledgegraph.zhongyao.domain.Medicine;
import com.knowledgegraph.zhongyao.service.IMedicineService;
import com.knowledgegraph.common.utils.poi.ExcelUtil;
import com.knowledgegraph.common.core.page.TableDataInfo;

/**
 * 药材维护Controller
 * 
 * @author ruoyi
 * @date 2023-07-03
 */
@RestController
@RequestMapping("/zhongyao/medicine")
public class MedicineController extends BaseController
{
    @Autowired
    private IMedicineService medicineService;
    @Autowired
    private IPrescriptionService prescriptionService;
    @Autowired
    private INeo4jNodeService neo4jNodeMapper;
    @Autowired
    public IGraphNodeService graphNodeService;
    /**
     * 查询药材维护列表
     */
    @PreAuthorize("@ss.hasPermi('zhongyao:medicine:list')")
    @GetMapping("/list")
    public TableDataInfo list(Medicine medicine)
    {
        startPage();
        List<Medicine> list = medicineService.selectMedicineList(medicine);
        return getDataTable(list);
    }

    /**
     * 导出药材维护列表
     */
    @PreAuthorize("@ss.hasPermi('zhongyao:medicine:export')")
    @Log(title = "药材维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Medicine medicine)
    {
        List<Medicine> list = medicineService.selectMedicineList(medicine);
        ExcelUtil<Medicine> util = new ExcelUtil<Medicine>(Medicine.class);
        util.exportExcel(response, list, "药材维护数据");
    }

    /**
     * 获取药材维护详细信息
     */
    @PreAuthorize("@ss.hasPermi('zhongyao:medicine:query')")
    @GetMapping(value = "/{medicineKey}")
    public AjaxResult getInfo(@PathVariable("medicineKey") String medicineKey)
    {
        return AjaxResult.success(medicineService.selectMedicineByMedicineKey(medicineKey));
    }

    /**
     * 新增药材维护
     */
    @PreAuthorize("@ss.hasPermi('zhongyao:medicine:add')")
    @Log(title = "药材维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Medicine medicine)
    {
        medicine.setMedicineKey(UUID.randomUUID().toString());
        GraphNode graphNode = new GraphNode();
        graphNode.setNodeType(2);
        graphNode.setGraphNodeKey(medicine.getMedicineKey());
        graphNode.setGraphKey("ide6297b84779a4510ab37d014c9b86b9b");
        graphNode.setGraphNodeName(medicine.getMedicineName());
        graphNode.setGraphNodeRemark(medicine.getFunction());
        graphNode.setCreateTime(new Date(System.currentTimeMillis()));
        graphNode.setGraphNodeUrl(medicine.getPicUrl());
        graphNode.setGraphNodeMyicon("el-icon-star-on");

        int i = neo4jNodeMapper.insertGraphNode(graphNode);

        graphNode.setGraphNodeId((long) i);
        return toAjax(medicineService.insertMedicine(medicine,graphNode));
    }

    /**
     * 修改药材维护
     */
    @PreAuthorize("@ss.hasPermi('zhongyao:medicine:edit')")
    @Log(title = "药材维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Medicine medicine)
    {
        Long a = prescriptionService.getid(medicine.getMedicineKey());
        GraphNode graphNode = new GraphNode();
        graphNode.setGraphNodeKey(medicine.getMedicineKey());
        graphNode.setGraphKey("ide6297b84779a4510ab37d014c9b86b9b");
        graphNode.setGraphNodeName(medicine.getMedicineName());
        graphNode.setGraphNodeRemark(medicine.getFunction());
        graphNode.setCreateTime(new Date(System.currentTimeMillis()));
        graphNode.setGraphNodeUrl(medicine.getPicUrl());
        graphNode.setGraphNodeMyicon("el-icon-star-on");
        graphNode.setGraphNodeId(a);
        neo4jNodeMapper.updateGraphNode(graphNode);
        return toAjax(medicineService.updateMedicine(medicine,graphNode));
    }

    /**
     * 删除药材维护
     */
    @PreAuthorize("@ss.hasPermi('zhongyao:medicine:remove')")
    @Log(title = "药材维护", businessType = BusinessType.DELETE)
	@DeleteMapping("/{medicineKeys}")
    public AjaxResult remove(@PathVariable String[] medicineKeys)
    {
        List<String> list = new ArrayList<>();

        if (medicineKeys.length > 0) {
            for (int i = 0; i < medicineKeys.length; i++) {
                Long GraphNodeId = prescriptionService.getid(medicineKeys[i]);
                list.add(String.valueOf(GraphNodeId));

            }


        }
        if (list != null && list.size() > 0) {
            neo4jNodeMapper.deleteGraphNodeByGraphNodeKeys(list.stream().toArray(String[]::new));
        }

        graphNodeService.deleteGraphNodeByGraphNodeKeys(medicineKeys);
        return toAjax(medicineService.deleteMedicineByMedicineKeys(medicineKeys));
    }
}
